﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Iit.ITBI.MDSManager
{
    class CustomAttribute : CustomObject
    {
        
        public CustomAttribute(string Name, Guid MUID) :base(Name,MUID) { }        

        public CustomAttribute(DataRow AttributeDetailesData, SlaveOrMaster EntityType, MDSQueryVersion QueryVersion)
        {
            if (QueryVersion == MDSQueryVersion.GetAttributeDetailesV1)
            {
                switch (EntityType)
                {
                    case SlaveOrMaster.Master:
                        base.Name = AttributeDetailesData["MasterAttribute_Name"].ToString();
                        if (AttributeDetailesData["MasterAttribute_MUID"].ToString().Length != 0)
                        {
                            base.MUID = (Guid)AttributeDetailesData["MasterAttribute_MUID"];
                        }
                        else
                        {
                            base.MUID = Guid.Empty;
                        }
                        break;

                    case SlaveOrMaster.Slave:

                        base.Name = AttributeDetailesData["SlaveAttribute_Name"].ToString();

                        ///if the Entity MUID is null, then the Entity does not exists in the slave model
                        ///therefore we update the Exists property accordantly, and we create masterAttributeMetadata new guid for later use
                        if (AttributeDetailesData["SlaveAttribute_MUID"].ToString().Length != 0)
                        {
                            base.MUID = (Guid)AttributeDetailesData["SlaveAttribute_MUID"];
                        }
                        else
                        {
                            base.MUID = new Guid();
                        }

                        break;
                }

                base.Initialize();
            }
            else
            {
                throw new NotSupportedQueryVersionException(string.Format("Query Version {0} is not supported.", QueryVersion.ToString()));
            }
        }

        public override string ToString()
        {
            StringBuilder str = new StringBuilder();

            str.AppendLine("Attribute: " + base.ToString());

            return str.ToString();
        }
                 
    }
}
